//
//  SEConfirmReserveImageCell.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/7.
//

import UIKit

class SEConfirmReserveImageCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = .background
        contentView.backgroundColor = .background
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func updateWithViewModel(_ viewModel: SEHospitalizeOrderDetailViewModel) {
        self.viewModel = viewModel
        collectionView.reloadData()
    }

    var viewModel = SEHospitalizeOrderDetailViewModel()
    
    @IBOutlet private weak var containerView: UIView!

    @IBOutlet weak var collectionViewHeight: NSLayoutConstraint! {
        didSet {
            collectionViewHeight.constant = (UIScreen.screenWidth - 15 * 6 - 30) / 3
        }
    }
    
    @IBOutlet weak var collectionView: UICollectionView! {
        didSet {
            collectionView.backgroundColor = .clear
            collectionView.delaysContentTouches = false
            collectionView.delegate = self
            collectionView.dataSource = self
            collectionView.register(SEImageCell.nibForCell(),
                                    forCellWithReuseIdentifier: SEImageCell.reuseIdentifier())
        }
    }
    
}

// MARK: - UICollectionViewDataSource

extension SEConfirmReserveImageCell: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return viewModel.numberOfItemsForImgCv
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SEImageCell.reuseIdentifier(), for: indexPath) as! SEImageCell
        cell.update(with: viewModel.infoViewModel, indexPath: indexPath, isCanEdit: false, deleteImage: nil)
        return cell
    }
}

// MARK: - UICollectionViewDelegate & UICollectionViewDelegateFlowLayout

extension SEConfirmReserveImageCell: UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let itemWH = (UIScreen.screenWidth - 15 * 6 - 30) / 3
        return CGSize(width: itemWH, height: itemWH)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 15
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 15
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
//        if indexPath.item == viewModel.numberOfItems {
//            delegate?.imageCell(didClickedPickerImage: self)
//        }
    }
    
    func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as? SEImageCell
        cell?.backgroundColor = .background
    }
    
    func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as? SEImageCell
        cell?.backgroundColor = .clear
    }
}
